# -*- coding:utf-8 -*-
"""
@Time : 2023/6/4 12:52
@Author : Gremore
@Des: 测试
"""
from fastapp.core.Auth import create_access_token
from fastapi.security.oauth2 import OAuth2PasswordRequestForm
from fastapi import Depends, HTTPException, Request

from fastapp.database.redis import sys_cache
from aioredis import Redis


async def test_oath2(data: OAuth2PasswordRequestForm = Depends()):
    user_type = False
    if not data.scopes:
        raise HTTPException(401, "请选择作用域!")
    if "is_admin" in data.scopes:
        user_type = True
    jwt_data = {
        "user_id": data.client_id,
        "user_type": user_type
    }
    jwt_token = create_access_token(data=jwt_data)

    return {"access_token": jwt_token, "token_type": "bearer"}
async def say_hello():
    return {"message": "Hello World"}


# 方式二：连接池放在依赖注入
async def test_redis_depends(today:int, cache: Redis = Depends(sys_cache)):
    await cache.set(name="today", value=today)
    value = await cache.get("today")
    return {"message": f"今天是{today}号, data={value}"}

# 方式一：连接池放在request
async def test_redis(req: Request, today:int):
    await req.app.state.cache.set(name="today", value=today)
    value = await req.app.state.cache.get("today")
    return {"message": f"今天是{today}号, data={value}"}


from fastapp.models.base import User
from fastapp.core.Response import success, fail
async def test_readdb(req: Request):
    user_list = await User().all().values()
    for user in user_list:
        print(f"用户:{user.get('username')}", user)
    return success(msg="用户信息", data=user_list)